# main.py
from fastapi import FastAPI
from src.render.router import router as render_router
from src.template.router import router as template_router
from fastapi.middleware.cors import CORSMiddleware
from src.config import (
    MINIO_BUCKET_NAME,
)
from .libs.s3_client import ensure_bucket
from .database import init_db

app = FastAPI(title="DocxTemplate API", version="1.0.0")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(render_router)
app.include_router(template_router)

init_db()


def lifespan(app: FastAPI):
    async def on_startup():
        ensure_bucket(MINIO_BUCKET_NAME)

    async def on_shutdown():
        print("Shutting down...")

    return {"startup": on_startup, "shutdown": on_shutdown}
